﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using com.huawei.esdk.tp.professional.local;
using com.huawei.esdk.tp.professional.DataType;

namespace eSDK_TP_Demo_CS_CSharp
{
    public class ConferenceService
    {
        private ConferenceInfoEx scheduleConf = null;
        private ConferenceMgrServiceEx service = null;

        public ConferenceService()
        {
            service = ConferenceMgrServiceEx.Instance();
            scheduleConf = new ConferenceInfoEx();
        }

        /// <summary>
        /// 添加‘预约会议’
        /// </summary>
        /// <returns></returns>
        public ResultCodeClass AddScheduleConf(string name, DateTime beginTime, string duration, string accessCode, string password,
            string mediaEncryptType, string cpResource, string auxVideoFormat, string auxVideoProtocol, string rate, SiteInfoEx[] sites)
        {
            ResultCodeClass resultC = new ResultCodeClass(); 
            //会议名称
            scheduleConf.name = name;
            //设置会议开始时间
            scheduleConf.beginTime = beginTime;
            //会议时长
            int minutes = Int32.Parse(duration);
            int hours = minutes / 60;
            minutes = minutes - hours * 60;
            scheduleConf.duration = "P0Y0M0DT" + hours.ToString() + "H" + minutes.ToString() + "M0.000S";
            //接入码
            scheduleConf.accessCode = accessCode;
            //密码
            scheduleConf.password = password;
            //加密方式
            scheduleConf.mediaEncryptType = Int32.Parse(mediaEncryptType);
            //多画面模式
            scheduleConf.cpResouce = Int32.Parse(cpResource);
            //辅流视频模式
            scheduleConf.auxVideoFormat = Int32.Parse(auxVideoFormat);
            //视频协议
            scheduleConf.auxVideoProtocol = Int32.Parse(auxVideoProtocol);
            //会议速率
            scheduleConf.rate = rate;
            //向会议中添加会场
            scheduleConf.sites = sites;
            //调用会议服务的scheduleConfEx方法预约会议，返回TPSDKResponseEx<ConferenceInfoEx>对象
            TPSDKResponseEx<ConferenceInfoEx> result = service.scheduleConfEx(scheduleConf);
            //调用TPSDKResponseEx<T>中的resultCode方法，获取返回码，如果返回码为0，则表示成功，否则，表示失败，具体失败原因，请参考错误码列表。
            int resultCode = result.resultCode;
            if (0 == resultCode)
            {
                //预约成功，则返回预约后的会议信息
                resultC.ConfInfo = result.result;
            }
            resultC.Result = resultCode.ToString();
            return resultC;
        }

        /// <summary>
        /// 向会议中添加会场
        /// </summary>
        /// <param name="name">会场名</param>
        /// <param name="confID">会议ID</param>
        /// <param name="beginTime">会议开始时间</param>
        /// <param name="siteType">会场类型</param>
        /// <param name="siteUri">会场URI</param>
        /// <returns>结果码</returns>
        public int AddSiteToConf(string name, string confID, DateTime beginTime, int siteType, string siteUri)
        {
            //DateTime beginTime,, string siteFrom,string dialingMode, string videoFormat, string videoProtocol, string rate, string status
            //新建一个SiteInfoEX对象 
            SiteInfoEx siteInfo = new SiteInfoEx();
            siteInfo.name = name;
            //会场URI
            siteInfo.uri = siteUri;
            siteInfo.type = siteType;
            //siteInfo.from = Int32.Parse(siteFrom);
            //siteInfo.dialingMode = Int32.Parse(dialingMode);
            //siteInfo.rate = rate;
            //siteInfo.videoFormat = Int32.Parse(videoFormat);
            //siteInfo.videoProtocol = Int32.Parse(videoProtocol);
            //siteInfo.status = Int32.Parse(status);
            TPSDKResponseEx<List<SiteAccessInfoEx>> result = service.addSiteToConfEx(confID, siteInfo, beginTime);
            //调用TPSDKResponseEx<T>中的resultCode方法，获取返回码，如果返回码为0，则表示成功，否则，表示失败，具体失败原因，请参考错误码列表。 
            int resultCode = result.resultCode;
            if (0 == resultCode)
            {
                //添加成功后，返回新会场接入到会议中的接入号信息 
                List<SiteAccessInfoEx> list = result.result;
            }
            return resultCode;
        }

        /// <summary>
        /// 呼叫会场
        /// </summary>
        /// <param name="confID">会议ID</param>
        /// <param name="list">会场名称</param>
        /// <returns>呼叫结果</returns>
        public int ConnectSites(string confID, List<string> list)
        {
            //如果返回值为0，则表示呼叫成功，否则，表示呼叫失败，具体失败原因，请参考错误码列表。 
            int resultCode = service.connectSitesEx(confID, list);
            return resultCode;
        }

        /// <summary>
        /// 移除会议中的会场
        /// </summary>
        /// <param name="confID">会议ID</param>
        /// <param name="uri">需要移除的会场URI</param>
        /// <param name="beginTime">会议开始时间</param>
        /// <returns>操作码</returns>
        public int DelSiteFromConf(string confID, string uri, DateTime beginTime)
        {
            int resultCode = service.delSiteFromConfEx(confID, uri, beginTime);
            return resultCode;
        }

        /// <summary>
        /// 查询所有会场信息
        /// </summary>
        /// <returns>所有会场信息</returns>
        public List<SiteInfoEx> QuerySites()
        {
            List<SiteInfoEx> sites = null;
            //调用会议服务中的querySitesEx方法查询所有会场信息，返回TPSDKResponseEx< List<SiteInfoEx>>对象。 
            TPSDKResponseEx<List<SiteInfoEx>> result = service.querySitesEx();
            //调用TPSDKResponseEx<T>中的resultCode方法，获取返回码，如果返回码为0，则表示成功，否则，表示失败，具体失败原因，请参考错误码列表。 
            int resultCode = result.resultCode;
            if (0 == resultCode)
            {
                //如果查询成功，则返回所有会场信息 
                sites = result.result;
            }

            return sites;
        }

        /// <summary>
        /// 查询指定会议的所有会场状态
        /// </summary>
        /// <param name="confId">会议ID</param>
        public QueryResultCode QueryConfSitesStatus(string confId)
        {
            QueryResultCode queryResultCode = new QueryResultCode();
            TPSDKResponseEx<List<SiteStatusEx>> result = service.queryConfSitesStatusEx(confId, null);
            int resultCode = result.resultCode;
            queryResultCode.Result = resultCode;
            if (0 == resultCode)
            {
                //如果操作成功，则返回List<SiteStatusEx>对象
                queryResultCode.SiteStatusList = result.result;
            }
            return queryResultCode;
        }
    }
}
